<template>
    <div class="m-order-item">
        <div class="m-order-type">
            <!-- pay_status:支付状态（-1 取消订单 0：未支付，1：已支付 （待发货） 2：待收货，3：确认收货 4：关闭订单） -->
            <span v-if="order.pay_status==0">等待买家付款</span>
            <span v-if="order.pay_status==1">待发货</span>
            <span v-if="order.pay_status==2">待收货</span>
            <span v-if="order.pay_status==3">确认收货</span>
            <span v-if="order.pay_status==4">订单已关闭</span>
            <em>2018-12-20</em>
        </div>
        <p class="m-orderNo">订单号：{{order.order_sn}}</p>
        
        <order-goodsinfo @click.native="toOrderDetail(order.id,order.sourceFrom)"
        v-for="(item,index) in order.goodsInfo" :key="index" 
        :goodsInfo="item"></order-goodsinfo>

        <div class="m-order-right">
            合计：<em>&yen;{{order.total_amount}}</em>
        </div>
        <!--未支付 -->
        <div class="m-order-right" v-if="order.pay_status=='0'">
            <button @click="cancleOrder(order.id)">取消订单</button>
            <button class="on" @click="toPayment">付款</button>
        </div>
        <!-- 待发货 -->
        <div class="m-order-right" v-if="order.pay_status==1">
            <button>等待买家发货</button>
        </div>
        <!-- 待收货 -->
        <div class="m-order-right" v-if="order.pay_status==2">
            <button class="on" @click="updateOrder(order.id)">确认收货</button>
        </div>
        <!-- 确认收货 -->
        <div class="m-order-right" v-if="order.pay_status==3">
            <button class="on" @click="toElevation">待评价</button>
        </div>
    </div>
</template>
<script>
import {mapState,mapMutations,mapActions} from 'vuex';
import {delOrder,sureOrder} from '@/api/mine';
import { error } from 'util';
export default {
    name:'',
    props:['order','orderIdx'],
    data () {
        return {

        }
    },
    computed:{
        ...mapState({
            buydata:state=>state.projectBuydata.buydata,
        })
    },
    components:{
        orderGoodsinfo:()=>import('./orderGoodsinfo'),
    },
    methods: {
        ...mapActions(['actionBuydata']),
        // 取消订单
        cancleOrder(order_id){
            let that = this;
            that.$messagebox.confirm('确定取消该订单？').then(action=>{
                delOrder({
                    customer_id:that.$utils.uid,
                    order_id:order_id
                }).then((result) => {
                    that.$toast('取消订单成功');
                    that.$emit('cancleOrder',that.orderIdx);
                }).catch((err) => {
                    
                });
            }).catch(err=>{console.log('取消')});
            
        },
        // 确认收货
        updateOrder(id) {
            let that = this;
            sureOrder({
                customer_id:that.$utils.uid,
                order_id:id
            }).then(result=>{
                that.$toast('您已确认收货');
                that.$router.go(0); //当前页面刷新
            }).catch(error=>{})
        },
        // 订单详情
        toOrderDetail(id,sourceFrom) {
            this.$router.push({path:'/orderDetail',query:{order_id:id,sourceFrom:sourceFrom}});
        },
        // 付款页面
        toPayment() {
            let goodsInfo = this.order.goodsInfo;
            let buydata = [];
            for (const key in goodsInfo) {
                buydata[key] = {
                    Id:goodsInfo[key].Id,
                    type:goodsInfo[key].type,
                    quantity:goodsInfo[key].quantity,
                }
            }
            this.actionBuydata(buydata);        //将buydata存入store
            this.$router.push({name:'payment'});
        },
        // 去评价
        toElevation() {
            let order_id = this.order.id;
            let sourceFrom = this.order.sourceFrom;
            let goodsInfo = this.order.goodsInfo;
            let buydata = [];
            for (const key in goodsInfo) {
                buydata.push({
                    Id:goodsInfo[key].Id,
                    type:goodsInfo[key].type,
                    quantity:goodsInfo[key].quantity,
                })
            }
            this.actionBuyData(buydata);        //将buydata存入store
            this.$router.push({path:'/mine/addEvaluation',query:{order_id:order_id,sourceFrom:sourceFrom}});
        }
    }
}
</script>
<style lang="less" scoped src="@/less/mine/myOrders.less"></style>
